// pages/category/category.js
import {getCategoryData} from '../../service/category'
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    leftMenuList: [],
    rightContentList: [],
    currentIndex: 0,
    scrollTop: 0
  },

  //---------事件监听---------

  selectMenu(e) {
    const index = e.currentTarget.dataset.index
    const rightContentList = this.data.categoryList[index].children
    this.setData({
      currentIndex: index,
      rightContentList,
      scrollTop: 0
    })

  },
  to_goods_list(e){
    const cid=e.currentTarget.dataset.cid
    wx.navigateTo({
      url: '/pages/goods_list/good_list?cid='+cid
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //请求分类数据
    this._getCategoryData()
  },
  //----------网络请求----------

  //请求分类数据

  //---------es7 async  await 写法----------
  async _getCategoryData() {
    const res = await getCategoryData()
    console.log(res)
    const categoryList = res.data.message
    //取出左侧菜单数据
    const leftMenuList = categoryList.map((v) => v.cat_name)
    //取出右侧内容数据
    const rightContentList = categoryList[0].children
    this.setData({
      leftMenuList: leftMenuList,
      rightContentList: rightContentList,
      categoryList: categoryList
    })
    //---------老版 写法----------
    // getCategoryData().then(res=>{
    //   console.log(res)
    //   //填充所有数据
    //   const categoryList=res.data.message
    //   //取出左侧菜单数据
    //   const leftMenuList=categoryList.map((v)=>v.cat_name)
    //    //取出右侧内容数据
    //    const rightContentList=categoryList[0].children
    //   this.setData({
    //     leftMenuList:leftMenuList,
    //     rightContentList:rightContentList,
    //     categoryList:categoryList
    //   })
    // })
  },


})